/*
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-09-10 10:57:39
 * @LastEditors: 颜礼田 2564217790@qq.com
 * @LastEditTime: 2024-09-10 11:50:24
 * @FilePath: /webServer/src/controller/department/department.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Post,
  Put,
  Query,
} from '@midwayjs/core';
import { DepartmentService } from '../../service/department/department.service';
import { Validate } from '@midwayjs/validate';
import { ListParamsValidator, StrIdValidator } from '../../validator';
import {
  EditDepartmentValidator,
  SaveDepartmentValidator,
} from '../../validator/department/department';
import { DepartmentSchema } from '../../schema';

@Controller('/department', { tagName: '' })
export class DepartmentController {
  @Inject()
  departmentService: DepartmentService;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<DepartmentSchema>) {
    const data = await this.departmentService.list(body);
    return Object.assign(data, body);
  }

  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveDepartmentValidator) {
    return await this.departmentService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.departmentService.info(body.id);
  }

  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.departmentService.remove(body.id);
  }

  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.departmentService.realRemove(body.id);
  }

  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(
    @Query() query: StrIdValidator,
    @Body() body: EditDepartmentValidator
  ) {
    const department = await this.departmentService.info(query.id);
    Object.assign(department, body);
    return await this.departmentService.save(department);
  }

  @Get('/trees')
  @Validate()
  async trees() {
    const list = await this.departmentService.trees();
    return { list };
  }
}
